<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业相关API调用Controller v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\enterprise;

use app\core\common\bean\BeanController;
use app\core\common\bean\BFC;
use app\core\model\EnterpriseCustomize;
use app\core\model\EnterpriseSiteConfig;
use app\core\model\SiteConfig;


/**
 * 企业自定义
 *
 * @package app\core\controller\enterprise
 */
class CustomizeController extends EnterpriseBaseController {

    use BeanController;

    public function __construct() {
        parent::__construct();
        //检查企业管理员
        $this->_checkEnterpriseAdmin();
    }

    protected function _getBeanName() {
        return "enterprise_customize";
    }

    protected function _getBeanFieldsConfig() {
        $configs = $this->_getDefaultBeanFieldsConfig();

        $configs['module'][BFC::WIDTH] = 100;
        $configs['controller'][BFC::WIDTH] = 200;
        $configs['action'][BFC::WIDTH] = 100;
        $configs['langset'][BFC::WIDTH] = 100;
        $configs['type'][BFC::IGNORE] = true;
        $configs['type_str'] = [
            BFC::VIRTUAL => true,
            BFC::TITLE => '类型',
            BFC::WIDTH => 100
        ];

        return $configs;
    }

    protected function _rowPreProcess(&$row) {
        $row['type_str'] = fetch(EnterpriseCustomize::$TYPES, $row['type'], $row['type']);
    }

    protected function _getQueryConditions($fieldsConfig) {
        $conditions = $this->_getDefaultQueryConditions($fieldsConfig);

        $type = input("get.type");
        $types = explode(",", $type);
        if (!empty($types)) {
            $conditions['type'] = ['in', $types];
        }

        return $conditions;
    }

    protected function _getGlobalBtns() {
        return [
            'add' => [
                BFC::TITLE => lang("ADD"),
                BFC::ICON => 'add-1',
                BFC::DLG => [
                    BFC::DLG_RELOAD => BFC::RELOAD_ALL
                ],
                BFC::ACTION => "add?type=" . input("get.type")
            ]
        ];
    }

    protected function _getRowBtns() {
        return [
            'edit' => [
                BFC::TITLE => lang("EDIT"),
                BFC::ICON => 'edit',
                BFC::DLG => [
                    BFC::DLG_RELOAD => BFC::RELOAD_ROW
                ],
                BFC::ACTION => 'edit?id=' . self::getParamPlaceholder("id")
            ],
            'duplicate' => [
                BFC::TITLE => lang("DUPLICATE"),
                BFC::ICON => 'templeate-1',
                BFC::DLG => [
                    BFC::DLG_RELOAD => BFC::RELOAD_ALL
                ],
                BFC::ACTION => 'add?type=' . self::getParamPlaceholder("type")
                    . '&tplId=' . self::getParamPlaceholder("id")
            ],
            'del' => [
                BFC::TITLE => lang("DEL"),
                BFC::ICON => 'delete',
                BFC::CLAZZ => 'layui-bg-red',
                BFC::AJAX => [
                    BFC::AJAX_RELOAD => BFC::RELOAD_ALL
                ]
            ]
        ];
    }

    protected function _getTableHeight() {
        return "full-20";
    }

    /**
     * @return \think\response\View
     */
    public function index() {
        return view();
    }

    /**
     * 企业站点配置
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function siteConfig() {
        if (is_submit()) {
            $cfs = input();

            foreach ($cfs as $name => $value) {
                EnterpriseSiteConfig::set($this->eid, $name, $value, false);
            }
            EnterpriseSiteConfig::reload($this->eid);
            $this->success("保存成功");
        } else {
            $cfs = SiteConfig::$cfs;
            $configValues = EnterpriseSiteConfig::get($this->eid);

            $configGroups = [];
            foreach ($cfs as $name => $cf) {
                if (!isset($configGroups[$cf['group']])) {
                    $configGroups[$cf['group']] = [];
                }
                $cf = array_merge($cf, [
                    'name' => $name,
                    'value' => fetch($configValues, $name, $cf['defaultVal']),
                ]);
                if (!isset($cf['type'])) {
                    $cf['type'] = 'input';
                }
                if (!isset($cf['tip'])) {
                    $cf['tip'] = '';
                }
                $configGroups[$cf['group']][] = $cf;
            }
            $this->assign("configGroups", $configGroups);
            return view();
        }
    }

    protected function _beforeShowAdd() {
        $types = [];
        $type = input("get.type");
        $allowTypes = explode(",", $type);
        if (empty($allowTypes)) {
            $types = EnterpriseCustomize::$TYPES;
        } else {
            foreach ($allowTypes as $allowType) {
                $types[$allowType] = EnterpriseCustomize::$TYPES[$allowType];
            }
        }

        $tplId = input('get.tplId');
        $tpl = $this->_q()->where([
            'id' => $tplId,
            'enterprise_id' => $this->eid
        ])->find();

        $this->assign("tpl", $tpl);
        $this->assign("types", $types);
    }

    /**
     * 数据添加成功
     *
     * @param $row
     * @param $id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _afterAddSuccess($row, $id) {
        EnterpriseCustomize::clearCache($this->eid, $row['type']);
    }

    /**
     * 数据更新成功
     *
     * @param $oldRecord
     * @param $condition
     * @param $params
     * @param $effectRowCount
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _afterEditSuccess($oldRecord, $condition, $params, $effectRowCount) {
        EnterpriseCustomize::clearCache($this->eid, $oldRecord['type']);
        if (isset($params['type']) && $params['type'] != $oldRecord['type']) {
            EnterpriseCustomize::clearCache($this->eid, $params['type']);
        }
    }

    /**
     * 数据删除成功
     *
     * @param $oldRecord
     * @param $condition
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _afterDelSuccess($oldRecord, $condition) {
        EnterpriseCustomize::clearCache($this->eid, $oldRecord['type']);
    }
}